import { Button, Tooltip } from 'antd';

// import { getSorter, ColumnsRenderFun, DictionaryRenderFun, formatDecimal, DBZ_STATE, BZZ_CHECKSTATE } from '../common/Format';

import { SchemaManageAPI } from '@/components/SchemaComponent/SchemaManage/type';
import { EditOutlined, EyeOutlined, RollbackOutlined } from '@ant-design/icons';
import {
  BJCS,
  BJKJ,
  BJXJ,
  BZZ_CHECKSTATE,
  ColumnsRenderFun,
  DYLY,
  DictionaryRenderFun,
  GKZB,
  JZXCS,
  JZXTP,
  KJXYCG,
  SHTH_CHECKSTATE,
  SHZ_CHECKSTATE,
  XJCG,
  formatDecimal,
  getSorter,
} from '../../Acommon';
import { DBZ_STATE, WJBZZ_STATE } from '../common/Format';
import { getPosIDs, getUserID, tooltipLength } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { findValueFun } from '@/common';
import { KYFW_Type, KYWZ_Type, PURCHASE_Type, ZNGL_Type } from '../../ApplyManage/common/Format';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: Record<string, any>, record: Record<string, any>) => {
  let operationArray: any = [];
  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );
  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible:
        curRes?.['/purchaseSourceController/editSource'] &&
        ((record?.state == DBZ_STATE() && !record?.checkState && record?.ownerID == getUserID() && record?.ownPositionID == getPosIDs()) ||
          (record?.state == WJBZZ_STATE() &&
            record?.checkState == SHTH_CHECKSTATE() &&
            record?.ownerID == getUserID() &&
            record?.ownPositionID == getPosIDs())),
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onInvalid',
    //   isVisible:
    //     curRes['/purchaseSourceController/invalid'] &&
    //     record?.id &&
    //     record?.ownerID == getUserID() &&
    //     record?.ownPositionID == getPosIDs() &&
    //     (record?.state == WJBZZ_STATE() || (record?.state == DBZ_STATE() && record?.checkState == SHZ_CHECKSTATE())),
    // },
  ];

  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * @getDocumentationManageTableColumns
 * @description 文件编制数据表格信息展示和操作
 */
const getDocumentationManageTableColumns = (operation: any, curRes): Record<string, any>[] => {
  const { onEdit, onView } = operation;
  return [
    {
      title: '承办部门/团队', // 原【申请部门】
      dataIndex: 'ownDepartmentName', // own
      key: 'ownDepartmentName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.ownDepartmentName, b?.ownDepartmentName),
      width: 250,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '采购项目名称',
      dataIndex: 'applyTitle',
      key: 'applyTitle',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.applyTitle, b?.applyTitle),
      width: 220,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '标包名称',
      dataIndex: 'packName',
      key: 'packName',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.packName, b?.packName),
      width: 250,
      render: (text: string, record: Record<string, any>) => {
        return (
          <>
            <a
              onClick={() => {
                if (curRes?.['/purchaseSourceController/getSourceById/{id}/{currentUserID}']) {
                  onView(record);
                }
              }}
              style={{ color: '100%' }}
            >
              <Tooltip title={tooltipLength(`${text}(${record?.packCode})`, 50)}>
                <span className={pageStyles['over-flow-hidden']}>{`${text}(${record?.packCode})` || '--'}</span>
              </Tooltip>
            </a>
          </>
        );
      },
    },
    {
      title: '采购类别',
      dataIndex: 'supplyWay',
      key: 'supplyWay',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.supplyWay, b?.supplyWay),
      width: 140,
      render: (text: number) => {
        return text == 0 ? '月度计划' : text == 1 ? '零星采购' : text == 2 ? '实验平台' : '--';
      },
    },
    {
      title: '采购组织类型',
      dataIndex: 'procureOrgName',
      key: 'procureOrgName',
      sorter: (a: any, b: any) => getSorter(a?.procureOrgName, b?.procureOrgName),
      visible: true,
      width: 180,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '拟采用采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      sorter: (a: any, b: any) => getSorter(a?.procureRealName, b?.procureRealName),
      visible: true,
      width: 180,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '预算金额（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.expectSum, b?.expectSum),
      width: 180,
      render: (text: string) => {
        return (
          <>
            <span>{formatDecimal(text)}</span>
          </>
        );
      },
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      sorter: (a: any, b: any) => getSorter(a?.stateName, b?.stateName),
      visible: true,
      width: 160,
      render: (text: string) => DictionaryRenderFun({ text, isState: true }),
    },
    {
      title: '采购类别',
      dataIndex: 'purchCategory',
      key: 'purchCategory',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.purchCategory, b?.purchCategory),
      width: 220,
      render: (text: string) => {
        return <span>{findValueFun(PURCHASE_Type, text)}</span>;
      },
    },
    {
      title: '采购内容',
      dataIndex: 'purchContent',
      key: 'purchContent',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.purchContent, b?.purchContent),
      width: 220,
      render: (text: string, record) => {
        return (
          <span>
            {record?.purchCategory == 0
              ? findValueFun(KYWZ_Type, text)
              : record?.purchCategory == 1
              ? findValueFun(KYFW_Type, text)
              : record?.purchCategory == 2
              ? findValueFun(ZNGL_Type, text)
              : '--'}
          </span>
        );
      },
    },
    {
      title: '其他内容',
      dataIndex: 'otherContent',
      key: 'otherContent',
      visible: false,
      sorter: (a: any, b: any) => getSorter(a?.otherContent, b?.otherContent),
      width: 220,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '申请时间',
      dataIndex: 'ownTime',
      key: 'ownTime',
      sorter: (a: any, b: any) => getSorter(a?.ownTime, b?.ownTime),
      width: 160,
      visible: true,
      render: (text: string) => {
        if (text) {
          text = text?.split(' ')?.[0];
        } else {
          text = '--';
        }
        return ColumnsRenderFun({ text });
      },
    },
    {
      title: '申请人公司',
      dataIndex: 'ownCompanyName',
      key: 'ownCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.ownCompanyName > b?.ownCompanyName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '申请人部门',
      dataIndex: 'ownDepartmentName',
      key: 'ownDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.ownDepartmentName > b?.ownDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '申请人岗位',
      dataIndex: 'ownPositionName',
      key: 'ownPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.ownPositionName > b?.ownPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '申请人',
      dataIndex: 'ownerName',
      key: 'ownerName',
      sorter: (a: any, b: any) => getSorter(a?.ownerName, b?.ownerName),
      width: 120,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text }),
    },

    // {
    //     title: '招标文件',
    //     dataIndex: 'document',
    //     key: 'document',
    //     width: 160,
    //     visible: true,
    //     render: () => {
    //         return (<a>www.baidu.com</a>)
    //     },
    // },

    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 150,
      visible: true,
      unDrag: true,
      align: 'left',
      fixed: 'right',
      render: (_: string, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operation, record)}</div>;
      },
    },
  ];
};

/**
 * @getDocumentationManageTableHeaderActions
 * @description 文件编制表格顶部自定义按钮
 * @returns SchemaManageAPI.IActionBtnList[]
 */
const getDocumentationManageTableHeaderActions = (operation: any): SchemaManageAPI.IActionBtnList[] => {
  const { onTest } = operation;
  return [];
  return [
    {
      title: '公开/邀请招标',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(GKZB());
      },
    },
    {
      title: '竞争性磋商',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(JZXCS());
      },
    },
    {
      title: '竞争性谈判',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(JZXTP());
      },
    },
    {
      title: '询价采购',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(XJCG());
      },
    },
    {
      title: '单一来源',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(DYLY());
      },
    },
    {
      title: '框架协议征集',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(KJXYCG());
      },
    },
    {
      title: '便捷磋商',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(BJCS());
      },
    },
    {
      title: '便捷询价',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(BJXJ());
      },
    },
    {
      title: '便捷框架协议征集',
      visible: true,
      type: 'primary',
      onClick: () => {
        onTest(BJKJ());
      },
    },
    // {
    //     title: '添加',
    //     visible: true,
    //     type: 'primary',
    //     icon: <PlusOutlined />,
    //     onClick: () => { onAdd() },
    // },
  ];
};
export { getDocumentationManageTableColumns, getDocumentationManageTableHeaderActions };
